package io.renren.modules.biz.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.Assert;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.biz.entity.BalanceTopupEntity;
import io.renren.modules.biz.entity.CountDto;
import io.renren.modules.biz.entity.MemberEntity;
import io.renren.modules.biz.service.BalanceTopupService;
import io.renren.modules.biz.service.MemberService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户充值提现申请表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-09-03 14:51:31
 */
@RestController
@RequestMapping("biz/balancetopup")
public class BalanceTopupController {
	@Autowired
	private BalanceTopupService balanceTopupService;

	@Autowired
	private MemberService memberService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("biz:balancetopup:list")
	@SuppressWarnings("unchecked")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = balanceTopupService.queryPage(params);
		List<BalanceTopupEntity> list = (List<BalanceTopupEntity>) page.getList();
		if (CollectionUtils.isNotEmpty(list)) {
			for (BalanceTopupEntity balanceTopupEntity : list) {
				MemberEntity entity = memberService.selectById(balanceTopupEntity.getMemberId());
				balanceTopupEntity.setHeadImg(entity.getHeadImg());
				balanceTopupEntity.setNickname(entity.getNickname());
				balanceTopupEntity.setRealname(entity.getRealname());
				balanceTopupEntity.setBankCardNumber(entity.getBankCardNumber());
				balanceTopupEntity.setBankName(entity.getBankName());
				balanceTopupEntity.setBankNameBranch(entity.getBankNameBranch());
				balanceTopupEntity.setAlipay(entity.getAlipay());
				balanceTopupEntity.setAlipayImg(entity.getAlipayImg());
				balanceTopupEntity.setWechat(entity.getWechat());
				balanceTopupEntity.setWechatImg(entity.getWechatImg());
			}
		}

		String mobile = (String) params.get("mobile");
		String memberId = (String) params.get("memberId");
		String authStatus = (String) params.get("authStatus");
		String startDate = (String) params.get("startDate");
		String endDate = (String) params.get("endDate");
		CountDto countDto = balanceTopupService.sumTopupAmount(memberId, mobile, startDate, endDate, authStatus);

		return R.ok().put("page", page).put("countDto", countDto);
	}

	/**
	 * 保存
	 */
	@SysLog("充值提现")
	@RequestMapping("/save")
	@RequiresPermissions("biz:balancetopup:save")
	public R save(@RequestBody BalanceTopupEntity balanceTopup) {
		// 校验类型
		ValidatorUtils.validateEntity(balanceTopup);

		balanceTopupService.save(balanceTopup);

		return R.ok();
	}

	/**
	 * 充提审核
	 */
	@SysLog("充提审核")
	@RequestMapping(path = "/changeStauts", method = RequestMethod.POST)
	@RequiresPermissions("biz:balancetopup:update")
	public R changeStauts(String type, Long[] ids) {
		Assert.hasText(type, "参数异常[type]");
		Assert.notNull(ids, "ids is null");
		Assert.noNullElements(ids, "参数异常[memberIds]");
		balanceTopupService.changeStauts(type, Arrays.asList(ids));
		return R.ok();
	}
}
